package com.yhg.qxmall.cartorder.service.impl;

import com.yhg.common.utils.R;
import com.yhg.qxmall.cartorder.feign.FrontWareService;
import com.yhg.qxmall.cartorder.vo.CartItemVo;
import com.yhg.qxmall.cartorder.vo.SkuStockTo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhg.common.utils.PageUtils;
import com.yhg.common.utils.Query;

import com.yhg.qxmall.cartorder.dao.FmsCartItemDao;
import com.yhg.qxmall.cartorder.entity.FmsCartItemEntity;
import com.yhg.qxmall.cartorder.service.FmsCartItemService;


@Service("fmsCartItemService")
public class FmsCartItemServiceImpl extends ServiceImpl<FmsCartItemDao, FmsCartItemEntity> implements FmsCartItemService {

    @Autowired
    FrontWareService wareService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<FmsCartItemEntity> page = this.page(
                new Query<FmsCartItemEntity>().getPage(params),
                new QueryWrapper<FmsCartItemEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void addToCart(CartItemVo vo, Long cartId) {
        //判断是否已经有该skuid
        FmsCartItemEntity RequestCartItem = this.baseMapper.selectOne(new QueryWrapper<FmsCartItemEntity>()
                .eq("sku_id", vo.getSkuId()).eq("cart_id", cartId));
        FmsCartItemEntity cartItemEntity = new FmsCartItemEntity();
        if (RequestCartItem==null){
            //没有商品
//            BeanUtils.copyProperties(vo,cartItemEntity);
//            cartItemEntity.setId();
            cartItemEntity.setCount(vo.getCount());
            cartItemEntity.setImage(vo.getImage());
            cartItemEntity.setMemberId(vo.getMemberId());
            cartItemEntity.setPrice(vo.getPrice());
            cartItemEntity.setSkuName(vo.getSkuName());
            cartItemEntity.setSkuId(vo.getSkuId());
            cartItemEntity.setCartId(cartId);
            this.baseMapper.insert(cartItemEntity);
        }else {
//            如果已经有了
            RequestCartItem.setCount(RequestCartItem.getCount()+vo.getCount());
            this.baseMapper.updateById(RequestCartItem);
        }
    }

    @Override
    public String createFromCart(Long memberId, Long skuId, Integer count) {
        //删除库存逻辑
//        调用远程服务查询库存是否存在充足
        SkuStockTo ware = wareService.getStock(skuId);
        if (ware!=null){
            Integer stock = ware.getStock();
            if (stock>count){
                //库存充足，加入订单
                //1.删除购物车
                this.remove(new QueryWrapper<FmsCartItemEntity>().eq("member_id", memberId).eq("sku_id", skuId));
                //2.删除库存
                wareService.deleteStock(skuId, count);
                return "库存充足，下单成功！";
            }else return "库存不足，请择日购买。";
        }else {
            return "库存不足，请择日购买。";
        }
    }

    @Override
    public String deleteCartItem(Long memberId, Long skuId) {
        boolean remove = this.remove(new QueryWrapper<FmsCartItemEntity>().eq("member_id", memberId).eq("sku_id", skuId));
        if (remove){
            return "移除成功";
        }else return "移除失败";

    }
}